package com.piz.dao.user;

import java.util.List;

import com.piz.dao.common.PizHiberImpl;
import com.piz.dao.common.UnexpectedDBException;
import com.piz.model.User;


/**
 * @author prameetk.sharma
 *
 */
public class UserDao extends PizHiberImpl<User>{
	
	/**
	 * @param username
	 * @return
	 */
	public User findUserByUserName(String username)
	{
		try {
			
			String hqlQuery = "from User u where u.username ='"+username+"'";
			List<User> user = executeHQL(hqlQuery);
			if(user.size()>0 && user.get(0) != null)
				return user.get(0);
		} 
		catch (Exception e) {
			throw new UnexpectedDBException("DBException in finding user by name.",e);
		}
		
		return null;
	}
	
	public User findUserById(Long id)
	{
		try {
			return search(User.class, id);
		} catch (Exception e) {
			throw new UnexpectedDBException("DBException in finding user with id ="+id,e);
		}
	}

	/**
	 * @param user
	 * @return
	 */
	public Long saveUser(User user)
	{
		try {
			saveOrUpdate(user);
			return user.getId();
		} catch (Exception e) {
			throw new UnexpectedDBException("DBException in saving user by name.",e);
		}
	}
	
	/**
	 * @return
	 */
	public List<User> listUser() {
		try {
			String hqlQuery = "from User";
			return (List<User>) executeHQL(hqlQuery);
		} catch (Exception e) {
			throw new UnexpectedDBException("DBException in getting list of users.",e);
		}
		
	}

}
